package cz.cvut.manatee.client.concordance;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import cz.cvut.manatee.client.Resources;
import cz.cvut.manatee.shared.AttributItem;
import cz.cvut.manatee.shared.concordance.CollectedParameters;


public class SearchWidgetWrapper<T extends ConcordanceFormWidget> extends HorizontalPanel {
	final SearchWidgetManagementInterface<T> management;
	final T form;
	
	String name;
	
	Button moveUp;
	Button moveDown;
	Button remove;
	
	Label nameLabel;
	Label orderLabel;
	
	SearchWidgetWrapper(final T form, final SearchWidgetManagementInterface<T> management, String name) {
		this.management = management;
		this.form = form;
		this.name = name;
		
		moveUp = new Button("up");
		moveDown = new Button("down");
		remove = new Button("del");
		
		VerticalPanel description = new VerticalPanel();
		nameLabel  = new Label();
		orderLabel = new Label();
		
		nameLabel.addStyleName(Resources.INSTANCE.css().wrapperLabel());
		orderLabel.addStyleName(Resources.INSTANCE.css().wrapperOrder());
		description.add(nameLabel);
		description.add(orderLabel);
		
		
		this.refresh(this.position()+1, this.quantity()+1);
		
		moveUp.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SearchWidgetWrapper.this.management.moveUp(SearchWidgetWrapper.this);
				SearchWidgetWrapper.this.refresh();
			}
		});
		
		moveDown.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SearchWidgetWrapper.this.management.moveDown(SearchWidgetWrapper.this);
				SearchWidgetWrapper.this.refresh();
			}
		});
		
		remove.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SearchWidgetWrapper.this.management.remove(SearchWidgetWrapper.this);
				SearchWidgetWrapper.this.refresh();
			}
		});
		
		this.add(description);
		this.add(form);
		this.add(moveUp);
		this.add(moveDown);
		this.add(remove);
		
		this.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		
		this.setStylePrimaryName(Resources.INSTANCE.css().searchWidgetWrapper());
		form.addStyleName(Resources.INSTANCE.css().form());
	}
	
	
	protected void refreshButtons() {
		refreshButtons(this.position(), this.quantity());
	}
	
	protected void refreshButtons(int position, int quantity) {
		if (quantity <= 1) {
			moveUp.setVisible(false);
			moveDown.setVisible(false);
		} else {
			moveUp.setVisible(true);
			moveDown.setVisible(true);
		}
		
		if (position < 1) {
			moveUp.setEnabled(false);
		} else {
			moveUp.setEnabled(true);
		}
		
		if (position >= quantity-1) {
			moveDown.setEnabled(false);
		} else {
			moveDown.setEnabled(true);
		}
	}
	
	protected void refreshLabel() {
		refreshLabel(this.position(), this.quantity());
	}
	
	protected void refreshLabel(int position, int quantity) {
		nameLabel.setText(name);
		orderLabel.setText((position+1) + " of " + quantity);
		
		if (quantity > 1) {
			orderLabel.setVisible(true);
		} else {
			orderLabel.setVisible(false);
		}
	}
	
	protected void refresh(int position, int quantity) {
		this.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		this.refreshButtons(position, quantity);
		this.refreshLabel(position, quantity);
	}
	
	protected void refresh() {
		int position = this.position();
		int quantity = this.quantity();
		this.refreshButtons(position, quantity);
		this.refreshLabel(position, quantity);
	}
	
	protected int position() {
		return management.position(SearchWidgetWrapper.this);
	}
	
	protected int quantity() {
		return management.size();
	}
	
	public T getForm() {
		return this.form;
	}

	public void setAttributs(AttributItem[] attributs) {
		form.setAttributes(attributs);
	}

	public CollectedParameters getParameters() {
		return form.getParameters();
	}

	public void setParameters(CollectedParameters params) {
		form.setParameters(params);
	}

}
